﻿jQuery(document).ready(function () {
    initFilterHolidayRequest();
    InitFullCalendar();
    initFilterCalendarHolidayRequest();
});
var initFilterHolidayRequest = function () {
    $('#frFilterHolidayRequest').submit(function () {
        App.blockUI($("#ListRequest-content"), false);
        $.ajax({
            method: "GET",
            url: "/HolidayManagement/ListEmployeeRequest",
            data: $(this).serialize(),
            success: function (data) {
                $("#ListRequest-content").html(data);
                initDatatable();
                App.unblockUI($("#ListRequest-content"), false);
            },
            error: function () {
                $("#ListRequest-content").html("Load failed");
                App.unblockUI($("#ListRequest-content"), false);
            }
        });
        
        return false;
    });
};
var initFilterCalendarHolidayRequest = function () {
    $('#frFilterCalendarHolidayRequest').submit(function () {
        App.blockUI($("#fullcalendar-pendingrequest"), false);
        InitFullCalendar($(this).serialize());
        App.unblockUI($("#fullcalendar-pendingrequest"), false);
        return false;
    });
};
var initApproveModel = function () {
    $('#frApproveRequest').submit(function () {
        App.blockUI($("#frApproveRequest"), false);
            if ($('#reasontextarea').val() == "") {
                $('#msg-frApproveRequest').html('<div class="alert alert-danger">'
                    + '<button type="button" class="close" data-dismiss="alert">×</button>'
                    + '<strong>Error!</strong> Please enter reason'
                    + '</div>');
                $('#reasontextarea').focus();
                App.unblockUI($("#frApproveRequest"), false);
            } else {
                $.ajax({
                    method: "POST",
                    url: $('#frApproveRequest').attr("action"),
                    data: $(this).serialize(),
                    success: function (data) {
                        $("#ApproveRequest").modal('hide');
                        App.unblockUI($("#frApproveRequest"), false);
                        $('#frFilterHolidayRequest').submit();
                        $('#frFilterCalendarHolidayRequest').submit();
                        $("#msg-ListRequest-content").html(data);
                        loadCountPendingRequest();
                    },
                    error: function () {
                        $("#ListRequest-content").html("Load failed");
                        App.unblockUI($("#frApproveRequest"), false);
                    }
                });
            }
            return false;
    });
};
var initCancelModel = function () {
    $('#frCancelRequest').submit(function () {
        App.blockUI($("#frCancelRequest"), false);
        if ($('#reasontextarea').val() == "") {
            $('#msg-frApproveRequest').html('<div class="alert alert-danger">'
                + '<button type="button" class="close" data-dismiss="alert">×</button>'
                + '<strong>Error!</strong> Please enter reason'
                + '</div>');
            App.unblockUI($("#frCancelRequest"), false);
            $('#reasontextarea').focus();
        } else {
            $.ajax({
                method: "POST",
                url: "/HolidayManagement/CancelRequest",
                data: $(this).serialize(),
                success: function (data) {
                    $("#CancelRequest").modal('hide');
                    App.unblockUI($("#frCancelRequest"), false);
                    $('#frFilterHolidayRequest').submit();
                    $('#frFilterCalendarHolidayRequest').submit();
                    $("#msg-ListRequest-content").html(data);
                    loadCountPendingRequest();
                },
                error: function () {
                    App.unblockUI($("#frCancelRequest"), false);
                    $("#ListRequest-content").html("Load failed");
                }
            });
        }
        return false;
    });
};

function ViewApproveModal(id) {
    App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/HolidayManagement/ApproveRequest",
        data: { id: id },
        success: function (data) {
            $("#ApproveRequest").html(data);
            initApproveModel();
        },
        error: function () {
            $("#ApproveRequest").html("Load failed");
        }
    });
    $("#ApproveRequest").modal();
    App.unblockUI($("#pendingRequest-content"), false);
}

function ViewCancelModal(id) {
    App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/HolidayManagement/CancelRequest",
        data: { id: id },
        success: function (data) {
            $("#CancelRequest").html(data);
            initCancelModel();
        },
        error: function () {
            $("#CancelRequest").html("Load failed");
        }
    });
    $("#CancelRequest").modal();
    App.unblockUI($("#pendingRequest-content"), false);
}
function ViewHolidayRequest(id) {
    App.blockUI($("#pendingRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/HolidayManagement/ViewRequest",
        data: { id: id },
        success: function (data) {
            $("#ViewHolidayRequest").html(data);
        },
        error: function () {
            $("#ViewHolidayRequest").html("Load failed");
        }
    });
    $("#ViewHolidayRequest").modal();
    App.unblockUI($("#pendingRequest-content"), false);
}
function btnDecline_click() {
        $('#frApproveRequest').attr('action', '/HolidayManagement/DeclineRequest');
        $('#frApproveRequest').submit();
}
function btnAccept_Click() {
    App.blockUI($("#frApproveRequest"), true);
    $.ajax({
        method: "POST",
        url: "/HolidayManagement/CheckConflictRequest",
        data: { bookId: $('#bookId').val() },
        success: function(data) {
            if (data == '' || data == null) {
                App.unblockUI($("#frApproveRequest"), true);
                $('#frApproveRequest').submit();
            } else {
                $("#msg-frApproveRequest").html(data);
                App.unblockUI($("#frApproveRequest"), true);
            }
        },
        error: function() {
            $("#msg-frApproveRequest").html("Load failed");
            App.unblockUI($("#frApproveRequest"), false);
        }
    });
}

function SubmitFormFilter() {
    $('#frFilterHolidayRequest').submit();
}
function SubmitFormCalendarFilter() {
    $('#frFilterCalendarHolidayRequest').submit();
}


function InitFullCalendar(serilazedata) {
        if (!jQuery().fullCalendar) {
            return;
        }

        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();

        var h = {
            left: 'prev,next,today',
            center: 'title',
            right: ''
        };
    var events = [];
        $.ajax({
            method: "GET",
            data:serilazedata,
            url: "/HolidayManagement/GetListCalendarHolidayRequest",
            success: function (data) {
            var index = 9999;
                $('#fullcalendar-pendingrequest').fullCalendar('destroy'); // destroy the calendar
                $('#fullcalendar-pendingrequest').fullCalendar({
                    //re-initialize the calendar
                    disableDragging: false,
                    header: h,
                    editable: false,
                    events: data,
                    eventRender: function(event, element) {
                        element.find('.fc-event-inner').append(event.ButtonHtml);
                        element.find('.fc-event-time').html("");//.html(event.start.getMonth() + "/" + event.start.getDate() + "/" + event.start.getFullYear() + " - " + event.end.getMonth() + "/" + event.end.getDate() + "/" + event.end.getFullYear());
                        element.find('.fc-event-inner').addClass(event.StatusClass);
                        element.css("z-index", index);
                        index--;
                    },
                    buttonText: {
                        prev: '<i class="m-icon-swapleft m-icon-white"></i>',
                        next: '<i class="m-icon-swapright m-icon-white"></i>',
                        today: '<i class="icon-calendar"></i> To Day'
                    }
                    //,
                        //eventClick: function (calEvent, jsEvent, view) {
                        //    alert('Event: ' + calEvent.title);
                        //},
                });
        },
        error: function() {
            $("#ListRequest-content").html("Load failed");
            App.unblockUI($("#ListRequest-content"), false);
        }
    });
}